package edu.ucsd.ccdb.rightClickPopupUtil;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import edu.ucsd.ccdb.cytoSegPopup.CytosegDialog;
import edu.ucsd.irods.IRODSFileString;
import edu.ucsd.irods.IRODSOperation;

public class RightClickMenu extends JPopupMenu implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JTree tree;
	private JFrame frame;
	private IRODSFileString file;
	
	public RightClickMenu ( JTree tree, JFrame frame, IRODSFileString file ) {
	
		this.tree = tree;
		this.frame = frame;
		this.file = file;
		
		JMenuItem menuItem;
		
		add (""); // add an empty element 
		
		addSeparator();
		
        menuItem = new JMenuItem("Delete");
        menuItem.addActionListener(this);        
        add(menuItem);
        
        addSeparator();
        
        CutAndPasteActionListener cutPasteActionListener = new CutAndPasteActionListener(this, tree);
        
        menuItem = new JMenuItem("Cut");
        menuItem.addActionListener( cutPasteActionListener );
        add(menuItem);
        
        menuItem = new JMenuItem("Paste");
        menuItem.addActionListener( cutPasteActionListener );
        menuItem.setEnabled(false);
        add(menuItem); 
        
        addSeparator();
        
        menuItem = new JMenuItem("Launch Segtoseg...");
        menuItem.addActionListener(new LaunchCytosegActionListerner() );
        add (menuItem);
        
        addSeparator();
        
        menuItem = new JMenuItem("Create a new folder...");
        menuItem.addActionListener(new CreateNewFolderListener (frame, tree ) );
        add(menuItem);
        
        addSeparator();
        
		menuItem = new JMenuItem("Remove test");
		menuItem.addActionListener(new removeTest());
		add (menuItem);
	}

	/*
	 * (non-Javadoc) This to handle the delete functionality
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {

		TreePath selectedPath = tree.getSelectionPath();
		
		if (selectedPath == null ) return;
		
		// get the file . Too long, maybe wrong :)
		IRODSFileString file = (IRODSFileString) ((DefaultMutableTreeNode) ( selectedPath.getLastPathComponent() )).getUserObject();
		System.out.println ( "File click: " + file.getAbsolutePath());
		
		if ( file.isFile() ) {
		
			// get the IRODSFile with the given path
			try {
				System.out.println ( "Delete successfull? " + IRODSOperation.deleteFile(file.getAbsolutePath()) );
			} catch (NullPointerException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			// animation
			System.out.println ("Do removing ");
			DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
			
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
			
			model.removeNodeFromParent(node);			
		}
		else {
			//TODO delete a folder
			System.out.println ("Can't delete. Only delete file");
		}
 	}
	
	/*-------------------------HELPER FUNCTION --------------------------*/
	private class removeTest implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			
			System.out.println ("Do removing test. Only animation, not backend changed");
			DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
			
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
			
			model.removeNodeFromParent(node);			
		}
	}

	private class LaunchCytosegActionListerner implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			// get the home folder
			TreePath selectedPath = tree.getSelectionPath();
			
			if (selectedPath == null ) return;
			
			// get the file . Too long, maybe wrong :)
			IRODSFileString fileClicked= (IRODSFileString) ((DefaultMutableTreeNode) ( selectedPath.getLastPathComponent() )).getUserObject();
			System.out.println ( "File click: " + fileClicked.getAbsolutePath());
			
			if ( !fileClicked.isDirectory() ) {

				System.out.println ("Need to be a folder containing training data");
				return;
			}
			
			CytosegDialog cytosegDialog = new CytosegDialog(frame, fileClicked, file.getAbsolutePath() );
			
	        // show it
			cytosegDialog.setSize(new Dimension(300, 150));
			cytosegDialog.setLocationRelativeTo(frame);
			cytosegDialog.pack();	// set all components to preferred size
			cytosegDialog.setVisible(true);		
		}
		
	}
	
}
